// 
// Copyright (c) 2006-2008 Ben Motmans
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
//
// Author(s):
//    Ben Motmans <ben.motmans@gmail.com>
//

using System;
using System.Collections.Generic;

using Anculus.Core;

namespace Anculus.Core
{
	public static class ThreadDispatch
	{
		private static bool isInitialized;
		private static IDispatcher dispatcher;

		public static bool IsInitialized
		{
			get { return isInitialized; }
		}
		
		public static void Shutdown ()
		{
			if (isInitialized) {
				dispatcher.Shutdown ();
				dispatcher = null;
				
				isInitialized = false;
			}
		}

		public static ulong Dispatch (VoidDispatchHandler callback)
		{
			if (!isInitialized)
				Initialize ();
			
			return dispatcher.Dispatch (callback);
		}

		public static ulong Dispatch (ObjectDispatchHandler callback, object param)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.Dispatch (callback, param);
		}

		public static ulong DispatchAtInterval (int interval, VoidDispatchHandler callback)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.DispatchAtInterval (interval, callback);
		}

		public static ulong DispatchAtInterval (int interval, ObjectDispatchHandler callback, object param)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.DispatchAtInterval (interval, callback, param);
		}
	
		public static ulong DispatchWithDelay (int delay, VoidDispatchHandler callback)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.DispatchWithDelay (delay, callback);
		}

		public static ulong DispatchWithDelay (int delay, ObjectDispatchHandler callback, object param)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.DispatchWithDelay (delay, callback, param);
		}

		public static ulong SyncDispatch (VoidDispatchHandler callback)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.SyncDispatch (callback);
		}

		public static ulong SyncDispatch (ObjectDispatchHandler callback, object param)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.SyncDispatch (callback, param);
		}

		public static ulong SyncDispatchWithDelay (int delay, VoidDispatchHandler callback)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.SyncDispatchWithDelay (delay, callback);
		}

		public static ulong SyncDispatchWithDelay (int delay, ObjectDispatchHandler callback, object param)
		{
			if (!isInitialized)
				Initialize ();

			return dispatcher.SyncDispatchWithDelay (delay, callback, param);
		}

		public static void CancelDispatch (ulong uid)
		{
			if (!isInitialized)
				return; //uid cannot be valid if not running

			dispatcher.CancelDispatch (uid);
		}


		public static void ResetDelay (ulong uid)
		{
			if (!isInitialized)
				return; //uid cannot be valid if not running

			dispatcher.ResetDelay (uid);
		}

		public static void CancelDelay (ulong uid)
		{
			if (!isInitialized)
				return; //uid cannot be valid if not running

			dispatcher.CancelDelay (uid);
		}

		public static void ChangeDelay (ulong uid, int delay)
		{
			if (!isInitialized)
				return; //uid cannot be valid if not running

			dispatcher.ChangeDelay (uid, delay);
		}

		public static void ChangeInterval (ulong uid, int interval)
		{
			if (!isInitialized)
				return; //uid cannot be valid if not running

			dispatcher.ChangeInterval (uid, interval);
		}

		private static void Initialize ()
		{
			if (isInitialized)
				return;
			
			isInitialized = true;
			
			dispatcher = new TimedDispatcher ();
			dispatcher.Initialize ();
		}
	}
}
